/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package auctions;

import java.util.ArrayList;

/**
 *
 * @author hangsu
 */
public abstract class ScheduleAuctionhouse {
    
    protected int auctionNum;
    
    protected int bidderNum;
    
    protected AuctionHouseType house_type;
    
    protected AuctionType auction_type; /* only support all auctions of the same type */
    
    protected ArrayList<ScheduleAgent> agents; /* passed from outside */
    
    protected ArrayList<ScheduleAuction> auctions;
    
    protected ArrayList<BidderResult> bResults;
    
    protected ArrayList<AuctionResult> aResults;
    
    /*
     * Each child should implement this method.
     * Called after setAgents (and possibly other setXXX methods), run all 
     * the auctions and store results in auctions, bidders, and local results.
     */
    public abstract void startAuctions();
    
    public ScheduleAuctionhouse(int nAuctions, AuctionType tAuctions) {
        this.auctionNum = nAuctions;
        this.auction_type = tAuctions;
        this.auctions = new ArrayList<ScheduleAuction> ();
        this.bResults = new ArrayList<BidderResult> ();
        this.aResults = new ArrayList<AuctionResult> ();
        
        int auctionIdx;
        switch(this.auction_type) {
            case SEALEDBID:
                for(auctionIdx=0; auctionIdx<this.auctionNum; auctionIdx++)
                    /* Default paymentrule for sealedbid: 2nd price */
                    this.auctions.add(new ScheduleSealedbidAuction(auctionIdx, 2));
                break;
            case ASCENDING:
                for(auctionIdx=0; auctionIdx<this.auctionNum; auctionIdx++)
                    this.auctions.add(new ScheduleAscendingAuction(auctionIdx));
                break;
       
            default:
                break;
        }
    }
    
    public void setAgents(ArrayList<ScheduleAgent> agents) {
        this.agents = agents;
        this.bidderNum = this.agents.size();
        for (int i=0;i<this.auctionNum; i++)
            this.auctions.get(i).setAgents(agents);
    }
    
    public void setPaymentRules(int pRules) {
        if(this.auction_type!=AuctionType.SEALEDBID) {
            System.out.println("payment rule not supported for auction type: "
                    +this.auction_type.toString());
        }
        for(ScheduleAuction a : this.auctions)
            ((ScheduleSealedbidAuction)a).setPaymentRule(pRules);
        
    }
    
    public void setReservePrices(double[] reserves) {
        for (int i=0; i<this.auctionNum; i++)
            this.auctions.get(i).setReservePrice(reserves[i]);
    }
    
    public void clear() {
        for(ScheduleAuction a : this.auctions)
                a.clear();
        for(ScheduleAgent a : this.agents)
                a.clear();
        this.aResults.clear();
        this.bResults.clear();
    }
    
    public ArrayList<AuctionResult> getAuctionsResults() {
        return this.aResults;
    }
    
    public ArrayList<BidderResult> getBidderResults() {
        return this.bResults;
    }
    
    public void writeResult(String outputFile) {
        if(this.aResults.size()!=this.auctionNum 
                || this.bResults.size()!=this.bidderNum) {
            System.out.println("Auctions not finished.");
            return;
        }
        ScheduleAuctionsToXML writer = new ScheduleAuctionsToXML(aResults, bResults);
        writer.generateXML(outputFile);
    }
}
